/**
 * Created by gongxun on 17/1/18.
 */

var GsbdxTableStatus = function() {
    var that = {};

    //牌桌映像
    that.mTableStatus = null;

    //录像msg
    that.replayMsgs = null;

    //录像type
    that.replayTypes = null;

    //是否是断线重练
    that.isReconnect = false;

    //设置牌桌映像
    that.setTableStatus = function(tableStatus){
        that.mTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step != 0){
            that.isReconnect = false;
        }

        //在牛牛里面才触发
        if (mo.sceneHelper.currentLayerName != "gsbdx"){
            return;
        }

        that.updateTableStatus();
    };

    that.setReconnectStatus = function(tableStatus){
        that.mTableStatus = tableStatus;

        //记录是否是断线重连
        if (that.mTableStatus.Step == 0){
            that.isReconnect = true;
        }

        //更新用户信息
        that.updateUserInfo();

        //断线重连
        that.setReconnet();

        //ui状态
        that.setUIShowStatus();
    };

    that.updateTableStatus = function(){
        //更新用户信息
        that.updateUserInfo();

        that.updateStep();

        //ui状态
        that.setUIShowStatus();
    };

    that.updateUserInfo = function(){
        //牌局结束以后（剩余次数为0，step为0） 不显示用户ui变化
        if (that.mTableStatus.LeftPlayTimes == 0 && that.mTableStatus.Step == 0){
            return;
        }

        //收到大结算了 不处理用户变化
        if (mo.gsbdxTable.mGsbdxTableRoomEndLayer != null){
            return;
        }


        if (that.mTableStatus.TableID){
            var infos = [null, null, null, null, null];
            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                infos[seat] = that.mTableStatus["SeatUser" + (logicSeat + 1)];
            }
            mo.gsbdxTable.mGsbdxTableUILayer.setTablePlayerInfos(infos);
        }else{
            //录像可能没有
        }
    };

    //更新牌局信息
    that.updateStep = function(){
        if (that.mTableStatus.Step == 0){

        }else if (that.mTableStatus.Step == 1){

        }else if (that.mTableStatus.Step == 2){
            //加倍 显示牌墙
            if (that.mTableStatus.LastAction == null){
                //下注按钮
                var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
                mo.gsbdxTable.mGsbdxTableUILayer.setBetInfo(mySeatUser.NewActionList);

                //显示牌墙
                var cardWellNums = that.mTableStatus.UsedCards;
                mo.gsbdxTable.showCardWell(cardWellNums, true);

                //如果是庄家就直接发送值
                if (mySeatUser.IsHost){
                    that.sendMyBet(0);
                }
            }
        }else if (that.mTableStatus.Step == 3){
            var lastAction = that.mTableStatus.LastAction;
            var lastSeat = that.mTableStatus.LastActionSeat;
            var viewSeat = that.getViewForLogic(lastSeat - 1, that.mTableStatus.MySeat - 1);

            //发牌
            if (lastAction == null || lastAction.code == 1){
                //显示牌墙
                var cardWellNums = that.mTableStatus.UsedCards;
                mo.gsbdxTable.showCardWell(cardWellNums, false);

                //发牌
                var cardNumss = [null, null, null, null, null];
                var cardTypes = [null, null, null, null, null];
                var dices = [parseInt(that.mTableStatus.SZ / 10), that.mTableStatus.SZ % 10];
                var toViewSeat = 0;

                for (var seat = 0; seat < 5; seat++){
                    var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                    var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];

                    if (seatUser != null){
                        //庄
                        if (seatUser.IsHost){
                            toViewSeat = (((dices[0] + dices[1] - 1) % 5) + seat) % 5;
                        }
                    }
                }
                mo.gsbdxTable.setFapaiAnim(dices, toViewSeat, cardNumss, cardTypes, true);
            }

            //开牌
            if (lastAction != null && lastAction.code == 2){
                var cardNums = lastAction.Cards;
                var cardType = that.getCardType(lastAction.Cards);
                mo.gsbdxTable.setKaipaiSeat(viewSeat, cardNums, cardType);
            }
        }
    };

    that.setReconnet = function(){
        var tableStatus = mo.gsbdxTableStatus.mTableStatus;

        mo.gsbdxTable.updateLastPlayTimes();

        if (tableStatus.Step == 2){
            //下注按钮
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            mo.gsbdxTable.mGsbdxTableUILayer.setBetInfo(mySeatUser.NewActionList);
        }

        if (tableStatus.Step == 3 || tableStatus.Step == 2){
            //显示牌墙
            var cardWellNums = that.mTableStatus.UsedCards;
            mo.gsbdxTable.showCardWell(cardWellNums, false);
        }

        if (tableStatus.Step == 3){
            //发牌加赋值
            var cardNumss = [null, null, null, null, null];
            var cardTypes = [null, null, null, null, null];
            var dices = [that.mTableStatus.SZ % 10, parseInt(that.mTableStatus.SZ / 10)];
            var toViewSeat = 0;

            for (var seat = 0; seat < 5; seat++){
                var logicSeat = that.getLogicForView(seat, that.mTableStatus.MySeat - 1);
                var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];

                if (seatUser != null){
                    //牌
                    if (seatUser.LastAction != null && seatUser.LastAction.code == 2){
                        cardNumss[seat] = seatUser.LastAction.Cards;
                        cardTypes[seat] = that.getCardType(seatUser.LastAction.Cards);
                    }

                    //庄
                    if (seatUser.IsHost){
                        toViewSeat = (((dices[0] + dices[1] - 1) % 5) + seat) % 5;
                    }
                }
            }
            mo.gsbdxTable.setFapaiAnim(dices, toViewSeat, cardNumss, cardTypes, false);
        }
    };

    //设置ui该显示的状态
    that.setUIShowStatus = function(){
        if (that.mTableStatus.Step == 0){
            //非牌局
            mo.gsbdxTable.mGsbdxTableUILayer.intoShowIndex(11);

            mo.gsbdxTable.updateLastPlayTimes();
        }else if (that.mTableStatus.Step == 1){
            //抢庄
        }else if (that.mTableStatus.Step == 2){
            //选倍
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            if (mySeatUser.NewActionList.length > 0){
                mo.gsbdxTable.mGsbdxTableUILayer.intoShowIndex(21);
            }else{
                mo.gsbdxTable.mGsbdxTableUILayer.intoShowIndex(22);
            }
        }else if (that.mTableStatus.Step == 3){
            //选牌型
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            if (mySeatUser.NewActionList.length > 0){
                mo.gsbdxTable.mGsbdxTableUILayer.intoShowIndex(30);
            }else{
                mo.gsbdxTable.mGsbdxTableUILayer.intoShowIndex(31);
            }
        }
    };

    //选择自己下注筹码
    that.sendMyBet = function(index){
        if (that.mTableStatus.Step == 2){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            if (newActionList.length > index){
                mo.gameMsgManager.sendActionRequestgsbdx(newActionList[index].code, newActionList[index].Rate, newActionList[index].Cards);
            }
        }
    };

    //自动开牌
    that.sendKaiPai = function(){
        if (that.mTableStatus.Step == 3){
            var mySeatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
            var newActionList = mySeatUser.NewActionList;
            if (newActionList.length > 0){
                mo.gameMsgManager.sendActionRequestgsbdx(newActionList[0].code, newActionList[0].Rate, newActionList[0].Cards);
            }
        }
    };

    //转换cardType
    that.getCardType = function(cards){
        var card1 = cards[0];
        var card2 = cards[1];
        var addtemp = card1 + card2;
        addtemp = addtemp % 10;
        if (card1 == card2){
            if (card1 == 87){
                //金豹子
                return 21;
            }else{
                return 20;
            }
        }else{
            if (card1 == 87 || card2 == 87){
                return (card1 + card2 + 3) % 10 + 10;
            }else{
                return addtemp;
            }
        }
    };

    //获取自己是否准备
    that.getMyIsReady = function(){
        var seatUser = that.mTableStatus["SeatUser" + that.mTableStatus.MySeat];
        return seatUser.State == 1;
    };

    //获取牌墙状态
    that.getCardWellType = function(){
        return that.mTableStatus.LeftPlayTimes % 2;
        // return 0;
    };

    //获取玩家同ip信息
    that.getPlayerIpEqual = function(){
        var result = [];

        //记录ip
        var ipStruct = {};
        for (var i = 1; i <= 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + i];
            if (seatUser != null && seatUser.user_info != null){
                var userInfo = seatUser.user_info;
                if (mo.gameHelper.getIsJson(userInfo.DeviceJson)) {
                    var deviceJson = JSON.parse(userInfo.DeviceJson);
                    var ip = deviceJson.ip;
                    if (ip != null && ip != "" && ip != "127.0.0.1") {
                        if (ipStruct[ip] == undefined) {
                            ipStruct[ip] = [];
                        }
                        ipStruct[ip].push(userInfo.nickname);
                    }
                }
            }
        }

        //分解出出现两次的用户
        for(var key in ipStruct){
            if (ipStruct.hasOwnProperty(key)) {
                if (ipStruct[key].length > 1){
                    var nicks = ipStruct[key];
                    for (var i = 0; i < nicks.length; i++){
                        var item = {};
                        item.nick = nicks[i];
                        item.ip = key;
                        result.push(item);
                    }
                }
            }
        }

        return result;
    };

    //通过ui的返回玩家视图座位号
    that.getViewSeatForUid = function (uid) {
        var logicIndex = -1;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    logicIndex = i;
                }
            }
        }
        if (logicIndex != -1){
            return that.getViewForLogic(logicIndex, that.mTableStatus.MySeat - 1);
        }else{
            return -1;
        }
    };

    //通过用户id获取用户性别
    that.getSexForUid = function(uid){
        var sex = 0;
        for (var i = 0; i < 5; i++){
            var seatUser = that.mTableStatus["SeatUser" + (i + 1)];
            if (seatUser != null && seatUser.user_info != null){
                if (seatUser.user_info.userid == uid || seatUser.user_info.nickname == uid){
                    sex = seatUser.user_info.sex;
                }
            }
        }
        return sex;
    };

    //通过视图座位号返回用户性别
    that.getSexForViewSeat = function(view){
        var sex = 0;
        var logicSeat = that.getLogicForView(view, that.mTableStatus.MySeat - 1);
        var seatUser = that.mTableStatus["SeatUser" + (logicSeat + 1)];
        if (seatUser != null && seatUser.user_info != null){
            sex = seatUser.user_info.sex;
        }
        return sex;
    };

    //返回玩家人数
    that.getPlayerCount = function(){
        var count = 0;
        for (var i = 1; i <=5; i++){
            var userSeat = that.mTableStatus["SeatUser" + i];
            if (userSeat != null){
                count++;
            }
        }
        return count;
    };

    //加载录像
    that.setReplayMsg = function(rePlaygsbdx){
        that.replayMsgs = [];
        that.replayTypes = [];

        //设置录像时候自己的座位号
        var replayMySeat = 1;
        var roundEndedEvent = rePlaygsbdx.Result;
        for(var i = 1; i <= 5; i++){
            if (roundEndedEvent["SeatUser" + i] != null){
                if (roundEndedEvent["SeatUser" + i].user_info != null){
                    if (roundEndedEvent["SeatUser" + i].user_info.userid == mo.gameUserInfo.uid){
                        replayMySeat = i;
                        break;
                    }
                }
            }
        }
        roundEndedEvent.MySeat = replayMySeat;

        //牌桌映像数据
        for (var i = 0; i < rePlaygsbdx.Images.length; i++){
            var tableStatsgsbdx = rePlaygsbdx.Images[i];
            //修改MySeat
            tableStatsgsbdx.MySeat = replayMySeat;

            that.replayMsgs.push(tableStatsgsbdx);
            that.replayTypes.push(2106);
        }

        //牌桌结算数据
        that.replayMsgs.push(rePlaygsbdx.Result);
        that.replayTypes.push(2108);
    };

    //播放录像
    that.playReplay = function(){
        if (that.replayMsgs.length > 0){
            var msg = that.replayMsgs.shift();
            var type = that.replayTypes.shift();

            var response = null;
            if (type == 2106){
                //牌桌映象
                response = new GsbdxTableStatsgsbdx();

            }else if (type == 2108){
                //牌桌结算
                response = new GsbdxRoundEndedEventgsbdx();

            }

            if (response){
                response.handleMsg(msg);
                response.log();
            }else{
                cc.warn("type:" + type + "未注册");
            }
            return true;
        }else{
            cc.log("录像播放完毕");
            return false;
        }

    };

    that.getLogicForView = function(view, myLogicSeat){
        var logic = (view + 5 + myLogicSeat) % 5;
        return logic;
    };

    that.getViewForLogic = function(logic, myLogicSeat){
        var view = (logic + 5 - myLogicSeat) % 5;
        return view;
    };

    return that;
};

mo.gsbdxTableStatus = new GsbdxTableStatus();